#pragma once
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>

class inetaddr
{
private:
    void net2hostPort()
    {
        _port=::ntohs(_sock.sin_port);
    }
    void net2hostIP()
    {
        char buffer[64];
        const char* ret=::inet_ntop(AF_INET,&_sock,buffer,sizeof(buffer));
        _ip=buffer;
    }
public:
    inetaddr(){}
    inetaddr(const struct sockaddr_in& socket):_sock(socket)
    {
        net2hostIP();
        net2hostPort();
    }
    inetaddr(uint16_t port):_port(port),_ip("0.0.0.0")
    {
        _sock.sin_family=AF_INET;
        _sock.sin_port=::htons(_port);
        _sock.sin_addr.s_addr=INADDR_ANY;
    }

    void setaddr(struct sockaddr_in sock){
        _sock=sock;
        net2hostIP();
        net2hostPort();
    }

    struct sockaddr_in* sockaddr(){return &_sock;}
    socklen_t len(){return sizeof(_sock);}
    uint16_t port(){return _port;}
    const std::string& ip(){return _ip;}
    const std::string addr(){return _ip+':'+std::to_string(_port);}

private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in _sock;
};